package com.congee02.thread;

/**
 * @author congee02
 * @date 1/31/2023 4:53 PM
 */
public class ThreadAlternateExecutionTest {

    public static void main(String[] args) {
        
        int printTime = 80;
        
        AlternatePrintThread printThread = new AlternatePrintThread("西门", printTime);
        printThread.start();
        
        for (int i = 0 ; i < printTime ; i ++) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(" :" + "MAIN PRINT" + " , 线程名 = " + Thread.currentThread().getName());
        }
        
    }

    private static class AlternatePrintThread extends Thread {
        private transient final int printTime;
        private String printString;
        public AlternatePrintThread(String printString, int printTime) {
            this.printString = printString;
            this.printTime = printTime;
        }

        @Override
        public void run() {
            try {
                for (int i = 0 ; i < printTime ; i ++ ) {
                    System.out.println("第" + i + "次: " + printString + ", 线程名 = " + Thread.currentThread().getName());
                    sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
}
